package com.wgts.wgts.mvp.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerPhoneLoginActivityComponent;
import com.wgts.wgts.dagger.module.PhoneLoginActivityModule;
import com.wgts.wgts.mvp.presenter.IPhoneLoginActivityPersenter;
import com.wgts.wgts.mvp.presenter.imp.PhoneLoginActivityPersenter;
import com.wgts.wgts.mvp.view.IPhoneLoginActivityView;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.netmodle.resp.LoginEntity;
import com.wgts.wgts.thirdParty.push.PushTools;
import com.wgts.wgts.tools.AppInfor;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 忘记密码验证手机号页面
* */

public class PhoneLoginActivity extends BaseActivity implements IPhoneLoginActivityView {
    @Inject
    IPhoneLoginActivityPersenter persenter;
    @BindView(R.id.back_image)
    ImageView backImage;
    @BindView(R.id.login_phone_et)
    EditText loginPhoneEt;
    @BindView(R.id.phone_ly)
    RelativeLayout phoneLy;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.code_ly)
    RelativeLayout codeLy;
    @BindView(R.id.login_ly)
    RelativeLayout loginLy;

    @Override
    public void init() {
        DaggerPhoneLoginActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .phoneLoginActivityModule(new PhoneLoginActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.wgts_foget_password_phone_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

    }

    @Override
    public void loginResult(LoginEntity loginEntity) {
        dialogDismiss();
        //设置别名
        PushTools.getInstance().setAlias(this, loginEntity.alia);
        if (loginEntity.userStatus.equals("0")) {
            Intent intent = new Intent(PhoneLoginActivity.this, WriteInvitationCodeActivity.class);
            intent.putExtra("phone", loginPhoneEt.getText().toString());
            startActivity(intent);
            finish();
            AppInfor.getInstance().setAccessToken(loginEntity.accessToken);
        } else if (loginEntity.userStatus.equals("1")) {

            AppInfor.getInstance().setAccessToken(loginEntity.accessToken);
            AppInfor.getInstance().setRefreshToken(loginEntity.refreshToken);
            AppInfor.getInstance().setUserState(loginEntity.userRole);
            AppInfor.getInstance().setUserYQCode(loginEntity.userCode);
            AppInfor.getInstance().setInviteCode(loginEntity.userCode);

            Intent intent = new Intent(PhoneLoginActivity.this, MoHuMainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void countDowmTime(final int time) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getCode.setText(String.valueOf(time) + "s");
            }
        });
    }

    @Override
    public void countDownTimeOver() {
        getCode.setEnabled(true);
        getCode.setText("重新获取");
        persenter.removeCountDownTimeListener();
    }

    @Override
    public void sendSmsResult() {
        dialogDismiss();
        MoHuToast.showToast("发送成功");
        persenter.addCountDownTimeListener();
    }

    @Override
    public void phoneResult() {
        dialogDismiss();
        Intent intent = new Intent(PhoneLoginActivity.this, WgtsResetPasswordActivity.class);
        intent.putExtra(WgtsResetPasswordActivity.PHONE, loginPhoneEt.getText().toString().trim());
        intent.putExtra(WgtsResetPasswordActivity.CODE, codeEt.getText().toString().trim());
        startActivity(intent);
    }

    private void login() {
        if (loginPhoneEt.getText().toString() != null) {

            if (loginPhoneEt.getText().toString().length() != 11) {
                MoHuToast.showToast("请输入正确的手机号");
                return;
            }

        } else {
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }

        if (codeEt.getText().toString() != null && !codeEt.getText().toString().equals("")) {
            if (codeEt.getText().toString().length() != 4) {
                MoHuToast.showToast("请输入正确的验证码");
                return;
            }
        } else {
            MoHuToast.showToast("请输入正确的验证码");
            return;
        }

        showDialog();
        persenter.phoneAffirm(loginPhoneEt.getText().toString().trim(), "FORGET_PWD", codeEt.getText().toString().trim());
        //persenter.login(editPhone.getText().toString(), editVerificationCode.getText().toString());
    }

    private void sendSMS() {
        if (loginPhoneEt.getText().toString() != null) {

            if (loginPhoneEt.getText().toString().length() != 11) {
                MoHuToast.showToast("请输入正确的手机号");
                return;
            }

        } else {
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        getCode.setEnabled(false);
        showDialog();
        getCode.setText("正在发送");
        persenter.sendSms(loginPhoneEt.getText().toString(), "FORGET_PWD");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        persenter.removeCountDownTimeListener();
    }

    @Override
    public void onError(int status, String msg) {
        if (status == PhoneLoginActivityPersenter.SMS_ERROR){
            getCode.setEnabled(true);
            getCode.setText("获取验证码");
            dialogDismiss();
            MoHuToast.showToast("操作频繁，请稍后");
            return;
        }

        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @OnClick({R.id.back_image, R.id.get_code, R.id.login_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_image:
                finish();
                break;
            case R.id.get_code:
                sendSMS();
                break;
            case R.id.login_ly:
                login();
                break;
        }
    }
}
